import createAxios from '/@/utils/axios'

export const getActionUrl = (url: string) =>
    new Map([
        ['index', url + '/index'],
        ['add', url + '/add'],
        ['edit', url + '/edit'],
        ['del', url + '/del'],
        ['group', url + '/group'],
        ['edit_group', url + '/editGroup'],
        ['save', url + '/save'],
        ['get_sensitive_field_value', url + '/getSensitiveFieldValue'],
    ])

export function index(url: string) {
    const actionUrl = getActionUrl(url)
    return createAxios({
        url: actionUrl.get('index'),
        method: 'get',
    })
}

export function getData(url: string, action: string, id: string) {
    const actionUrl = getActionUrl(url)
    return createAxios({
        url: actionUrl.get(action),
        method: 'get',
        params: {
            id: id,
        },
    })
}

export function postData(url: string, action: string, data: anyObj) {
    const actionUrl = getActionUrl(url)
    return createAxios(
        {
            url: actionUrl.get(action),
            method: 'post',
            data: data,
        },
        {
            showSuccessMessage: true,
        }
    )
}

export function delData(url: string, ids: string[]) {
    const actionUrl = getActionUrl(url)
    return createAxios(
        {
            url: actionUrl.get('del'),
            method: 'DELETE',
            params: {
                ids: ids,
            },
        },
        {
            showSuccessMessage: true,
        }
    )
}

export function getConfigGroup(url: string) {
    const actionUrl = getActionUrl(url)
    return createAxios({
        url: actionUrl.get('group'),
        method: 'GET',
    })
}

export const getSensitiveFieldValue = (url: string, action: string, name: string) => {
    const actionUrl = getActionUrl(url)
    return createAxios({
        url: actionUrl.get('get_sensitive_field_value'),
        method: 'post',
        data: {
            name,
        },
    })
}
